package com.newear.happyDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.newear.R;
import com.newear.bean.ReportInfo;
import com.newear.util.HardWare;
import com.newear.util.Validator;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevideoplayer.TxVideoPlayerController;

import java.util.List;

/**
 * Created by happy on 2018/6/2.
 */

public class HealthPlanDialog extends AlertDialog {
    private ImageView closeImageView;
    private Context mContext;
    private Button btn_dialog_yuan_ze;//保健原则
    private Button btn_dialog_gong_fa;//中医功法
    private Button btn_dialog_an_ya;//穴位按压
    private Button btn_dialog_bao_jiang;//适宜保健品

    //对应的布局,保健原则
    private LinearLayout lL_one_yuan_ze;
    private TextView tv_one_yuan_ze;
    //对应的布局,中医功法
    private LinearLayout lL_two_gong_fa;
    //对应的布局,穴位按压
    private LinearLayout lL_three_an_ya;

    //对应的布局,适宜保健品
    private LinearLayout lL_four_bao_jiang;
    private ReportInfo reportInfo;

    public HealthPlanDialog(Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        //点击外面不允许消失
        setCanceledOnTouchOutside(false);
        getWindow().getAttributes().gravity = Gravity.CENTER;
    }

    public void setReportInfo(ReportInfo reportInfo) {
        this.reportInfo = reportInfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.health_plan_layout);
        init();
        findViews();
        setListeners();
        if (reportInfo == null) {
            return;
        }
        //保健原则
        addHealthGuidanceView();
        //中医功法视频
        addGongFaViews();
        //穴位按压
        addAnYaViews();
        //适宜保健品
        addBaoJiangViews();
    }

    private void init(){

    }

    /**
     * 保健原则
     */
    private void addHealthGuidanceView() {
        if (reportInfo.solutions == null) {
            return;
        }
        List<String> healthGuidances = reportInfo.solutions.healthGuidances;
        if (healthGuidances != null) {
            String healthGuidanceStr = "";
            for (int i = 0; i < healthGuidances.size(); i++) {
                healthGuidanceStr += healthGuidances.get(i) + "\n";
            }
            tv_one_yuan_ze.setText(healthGuidanceStr);
        }
    }

    private void findViews(){
        closeImageView = (ImageView) findViewById(R.id.img_plan_close);
        btn_dialog_yuan_ze = (Button) findViewById(R.id.btn_dialog_yuan_ze);
        btn_dialog_yuan_ze.setSelected(true);
        btn_dialog_gong_fa = (Button) findViewById(R.id.btn_dialog_gong_fa);
        btn_dialog_an_ya = (Button) findViewById(R.id.btn_dialog_an_ya);
        btn_dialog_bao_jiang = (Button) findViewById(R.id.btn_dialog_bao_jiang);

        //one
        lL_one_yuan_ze = (LinearLayout) findViewById(R.id.lL_one_yuan_ze);
        tv_one_yuan_ze = (TextView) findViewById(R.id.tv_one_yuan_ze);
        //two
        lL_two_gong_fa = (LinearLayout) findViewById(R.id.lL_two_gong_fa);
        //three
        lL_three_an_ya = (LinearLayout) findViewById(R.id.lL_three_an_ya);
        //four
        lL_four_bao_jiang = (LinearLayout) findViewById(R.id.lL_four_bao_jiang);
    }

    private void setListeners(){
        closeImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        //保健原则
        btn_dialog_yuan_ze.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_dialog_yuan_ze.setSelected(true);
                btn_dialog_gong_fa.setSelected(false);
                btn_dialog_an_ya.setSelected(false);
                btn_dialog_bao_jiang.setSelected(false);

                lL_one_yuan_ze.setVisibility(View.VISIBLE);
                lL_two_gong_fa.setVisibility(View.GONE);
                lL_three_an_ya.setVisibility(View.GONE);
                lL_four_bao_jiang.setVisibility(View.GONE);
                NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
                //下面展示数据逻辑
            }
        });
        //中医功法
        btn_dialog_gong_fa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_dialog_yuan_ze.setSelected(false);
                btn_dialog_gong_fa.setSelected(true);
                btn_dialog_an_ya.setSelected(false);
                btn_dialog_bao_jiang.setSelected(false);

                lL_one_yuan_ze.setVisibility(View.GONE);
                lL_two_gong_fa.setVisibility(View.VISIBLE);
                lL_three_an_ya.setVisibility(View.GONE);
                lL_four_bao_jiang.setVisibility(View.GONE);
            }
        });
        //穴位按压
        btn_dialog_an_ya.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_dialog_yuan_ze.setSelected(false);
                btn_dialog_gong_fa.setSelected(false);
                btn_dialog_an_ya.setSelected(true);
                btn_dialog_bao_jiang.setSelected(false);

                lL_one_yuan_ze.setVisibility(View.GONE);
                lL_two_gong_fa.setVisibility(View.GONE);
                lL_three_an_ya.setVisibility(View.VISIBLE);
                lL_four_bao_jiang.setVisibility(View.GONE);
                NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
            }
        });
        //适宜保健品
        btn_dialog_bao_jiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_dialog_yuan_ze.setSelected(false);
                btn_dialog_gong_fa.setSelected(false);
                btn_dialog_an_ya.setSelected(false);
                btn_dialog_bao_jiang.setSelected(true);

                lL_one_yuan_ze.setVisibility(View.GONE);
                lL_two_gong_fa.setVisibility(View.GONE);
                lL_three_an_ya.setVisibility(View.GONE);
                lL_four_bao_jiang.setVisibility(View.VISIBLE);
                NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
            }
        });

    }

    @Override
    protected void onStop() {
        super.onStop();
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
    }

    @Override
    public void onBackPressed() {
        if (NiceVideoPlayerManager.instance().onBackPressd()) return;
        super.onBackPressed();
    }

    private void addGongFaViews(){
        if (lL_two_gong_fa != null){
            lL_two_gong_fa.removeAllViews();
        }
        if (reportInfo.solutions == null) {
            return;
        }
        ReportInfo.Solutions solutions = reportInfo.solutions;
        if (solutions.videos == null) {
            return;
        }
        for (int i = 0; i < solutions.videos.size(); i++){
            View view = View.inflate(mContext, R.layout.plan_dialog_gong_fa_layout, null);
            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            ReportInfo.Video video = solutions.videos.get(i);
            if (Validator.isEffective(video.asset)) {
                final String url = solutions.assetPrefix + video.asset;
                Log.e("josan", "vedio url:" + url);
                NiceVideoPlayer mNiceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
                mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.TYPE_IJK); // or NiceVideoPlayer.TYPE_NATIVE
                //mNiceVideoPlayer.setUp("http://content-cdn.zhiyuntcm.com/%E5%8A%9F%E6%B3%95/%E7%97%B0%E6%B9%BF1.mp4", null);
                mNiceVideoPlayer.setUp(url, null);
                final TxVideoPlayerController controller = new TxVideoPlayerController(mContext);
                controller.setTitle(video.name);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        final Bitmap bitmap = HardWare.getNetVideoBitmap(url);
                        if (bitmap != null) {
                            ((Activity)mContext).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    controller.imageView().setImageBitmap(bitmap);
                                }
                            });
                        }
                    }
                }).start();
                mNiceVideoPlayer.setController(controller);
            }
            lL_two_gong_fa.addView(view);
        }
    }

    private void addAnYaViews(){
        if (reportInfo.solutions == null) {
            return;
        }
        ReportInfo.Solutions solutions = reportInfo.solutions;
        if (solutions.videos == null) {
            return;
        }
        if (lL_three_an_ya != null){
            lL_three_an_ya.removeAllViews();
        }
        List<ReportInfo.Massage> massageList = solutions.massages;
        if (massageList != null) {
            for (int i = 0; i < massageList.size(); i++) {
                ReportInfo.Massage massage = massageList.get(i);
                View view = View.inflate(mContext, R.layout.plan_dialog_an_ya_layout, null);
                ImageView imgAnYA = (ImageView) view.findViewById(R.id.img_three_an_ya);
                if (Validator.isEffective(massage.asset)) {
                    Glide.with(mContext).load(solutions.assetPrefix + "/" + massage.asset).into(imgAnYA);
                }
                TextView tvAnYA = (TextView) view.findViewById(R.id.tv_three_an_ya);
                view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                lL_three_an_ya.addView(view);
                tvAnYA.setText(massage.content);
            }
        }

    }

    private void addBaoJiangViews(){
        //根据服务器的数组
        if (lL_four_bao_jiang != null){
            lL_four_bao_jiang.removeAllViews();
        }
        if (reportInfo.healthProductList == null) {
            return;
        }
        List<ReportInfo.HealthProduct> healthProductList = reportInfo.healthProductList;
        for (int i = 0; i < healthProductList.size(); i++) {
            View view = View.inflate(mContext, R.layout.plan_dialog_bao_jiang_layout, null);
            ImageView imgBaoJiang = (ImageView) view.findViewById(R.id.img_four_bao_jiang);
            ReportInfo.HealthProduct healthProduct = healthProductList.get(i);
            String imageUrl = healthProduct.image;
            if (Validator.isEffective(imageUrl)) {
                Glide.with(mContext).load(imageUrl).into(imgBaoJiang);
            }
            TextView tvMainTitle = (TextView) view.findViewById(R.id.tv_four_title);
            tvMainTitle.setText(healthProduct.name);
            TextView tvBaoJiang = (TextView) view.findViewById(R.id.tv_four_bao_jiang);
            tvBaoJiang.setText(healthProduct.description);
            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            lL_four_bao_jiang.addView(view);

        }

    }





}
